package com.wtwd.campus.fjryE15.service.impl;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.H5Mapper;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.fjryE15.service.DeviceService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.utils.CommandNumberUtil;
import com.wtwd.campus.utils.CommandSendUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author mjy
 * @date 2022/09/26
 * @description
 */
@Service("fjryDeviceService")
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DeviceInfoService deviceInfoService;

    @Value("${h5.customerId.ryh5}")
    private Integer ryh5CustomerId;

    @Resource
    private CommandSendUtils commandSendUtils;

    @Resource
    private H5Mapper h5Mapper;


    @Override
    public Boolean authDeviceIsExist(String imei) {
        DeviceInfo device = deviceInfoService.getDeviceByImei(imei);
        if (device == null || device.getDeviceType() != Constants.H5_DEVICE || !device.getCustomerId().equals(ryh5CustomerId)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean unbindStudentCard(String imei) {
        DeviceInfo device = deviceInfoService.getDeviceByImei(imei);
        if (device == null) {
            return false;
        }
        h5Mapper.unbindDeviceByDevice(device.getDeviceId());
        String commandNumber = CommandNumberUtil.getCurTime();
        return commandSendUtils.reSetDevice(device.getImei(), commandNumber, true, Constants.H5_COMMAND_USER_ID, false);
    }

    @Override
    public boolean authDeviceIsOnline(String imei) {
        return commandSendUtils.deviceOnline(imei);
    }

}
